var app = getApp();
var Bmob = require('../../utils/Bmob-2.2.0.min.js');

var util = require('../../utils/util.js');
var pageData = {
  data: {
    swiperInfo: [],
    service: {},
    IconInfo: [],
    iamges: [],
    inputValue: '',
    serviceText: ["水电工", "家政保洁", "家电维修", "跑腿服务", "门窗维修", "送水"],
    hiddenmodalput: true,
    showGetUserInfo: true
   
  },
  need_login: false,
  
  onLoad: function (e) {
  
    this.get_swiperInfo();
    this.get_icon();
    this.get_service();
   // this.getseckill_goods();
 
  },
  topInfo_navigate: function (e) {
    var id = e.currentTarget.dataset.id;
    var links = e.currentTarget.dataset.links;
    var url = links  + id;

    wx.navigateTo({
      url: url
    });
  },



  get_swiperInfo: function () {
    var that = this;
   
    var swiper = Bmob.Query("swiper");
    swiper.equalTo("page_Id", '==','1');
    swiper.find().then(res=>{

        that.setData({
          swiperInfo: res
        })
      

    })


  },

  get_service: function () {
    var that = this;
 
    var service = Bmob.Query("appointsment");
    service.equalTo("status","==", 2);
    service.order("-createdAt");
    service.find().then(res=>{
    

        that.setData({
          services:res
        })
   

    })

  },

  getseckill_goods: function () {
    var that = this;
   
    var goodInfo =  Bmob.Query("goods");

    goodInfo.equalTo("state","==", 1);
    //goodInfo.descending('createdAt');  //排序

    goodInfo.find().then(res=>{
    
        console.log(res);
        var goods = [];
        for (var i = 0; i < res.length; i++) {

          var good = res[i];
        //  good.id = res[i].objectId;
          good.server_time = util.formatTime();
          good.downCount = {
            hours: '00',
            minutes: '00',
            seconds: '00'
          };
          if (good.seckill_start_state == 0) {
            that.downcount = app.beforeSeckillDownCount(good, that, 'goods_data[' + i + ']');
          } else if (good.seckill_start_state == 1) {
            that.downcount = app.duringSeckillDownCount(good, that, 'goods_data[' + i + ']');
          }
          var allstock = 0;
          for (var a = 0; a < good.models.length; a++) {
            var modelInfo = good.models[a];

            for (var j = 0; j < modelInfo.models.length; j++) {
              allstock += modelInfo.models[j].stock;

            }
          }
          good.seckill_progress = Math.round(good.sales / allstock);
          good.stock = allstock;
          good.seckill_progress = (good.sales / allstock * 100).toFixed(0);
          goods.push(good);
        }

        that.setData({
          goods_data: goods,

        })
      
  
  
    });
  },

  turnTo:function(e){
console.log(e);
var url=e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url

    })
  },
  get_icon: function () {
    var that = this;
    //var album5 = that.data.album5
  
    var icon = Bmob.Query("page_Icon");
    icon.equalTo("state","==", true);
    icon.equalTo("pageid","==", 0);
    icon.order("sepuence");
    icon.find().then(res=>{
   
        //album5.li=res;
        console.log(res);
        that.setData({
          IconInfo: res
        })

  
    })
  },

  game: function () {
    var that = this;
    //var album5 = that.data.album5
   
    var images = Bmob.Query("goods_image");
    var image_url = [];
    images.find().then(res=>{
   
        //album5.li=res;
        console.log(res);
        for (var i = 0; i < res.length; i++) {
          image_url.push(res[i].get("image_url"))

        }
        wx.previewImage({
          current: '', // 当前显示图片的http链接
          urls: image_url // 需要预览的图片http链接列表
        })
  
    })


  },
  bindKeyInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  modalinput: function () {
    this.setData({
      hiddenmodalput: !this.data.hiddenmodalput
    })
  },
  //取消按钮  
  cancel: function () {
    this.setData({
      hiddenmodalput: true
    });
  },
  

  dataInitial: function () {
    this.get_swiperInfo();
  
  },

  onShareAppMessage: function (res) {
  
   
    return {
      title: '邻舍边身边的便民服务',
      path: '/pages/page10000/page10000'
    }
  },
  onShow: function () {

  },
 
 
  tapPhoneCallHandler: function (e) {
    app.tapPhoneCallHandler(e);
  },
 
  turnToSeckillDetail: function (e) {
    console.log(e);
    app.turnToSeckillDetail(e);
  },

};
Page(pageData);
